﻿using Iworld.ViewModel;
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Iworld.Client.Framework
{
    /// <summary>
    /// 视图模型的基类
    /// </summary>
    public abstract class ViewModelBase : ModelBase
    {
        #region 私有参数

        bool isBusy = false;
        string viewTitle = "";
        string _error = "";
        string _msg = "";
        #endregion

        /// <summary>
        /// 包裹事件处理器，处理异常
        /// </summary>
        /// <typeparam name="TEventArgs"></typeparam>
        /// <param name="input"></param>
        /// <returns></returns>
        protected EventHandler<TEventArgs> WrapEventHandler<TEventArgs>(EventHandler<TEventArgs> input) where TEventArgs : EventArgs
        {
            return (object sender, TEventArgs args) =>
            {
                try
                {
                    input(sender, args);
                }
                catch (Exception ex)
                {
                    this.ShowError(ex.Message);
                }

            };
        }

        #region 公开属性

        /// <summary>
        /// 错误信息
        /// </summary>
        public string Error
        {
            get
            {
                return _error;
            }
            set
            {
                if (_error != value)
                {
                    _error = value;
                    OnPropertyChanged("Error");
                }
            }
        }

        /// <summary>
        /// 提示信息
        /// </summary>
        public string Msg
        {
            get
            {
                return _msg;
            }
            set
            {
                if (_msg != value)
                {
                    _msg = value;
                    OnPropertyChanged("Msg");
                }
            }
        }


        /// <summary>
        /// 一个布尔值 标识线程正忙
        /// </summary>
        public bool IsBusy
        {
            get
            {
                return isBusy;
            }
            set
            {
                if (isBusy != value)
                {
                    isBusy = value;
                    OnPropertyChanged("IsBusy");
                }
            }
        }

        /// <summary>
        /// 界面标题
        /// </summary>
        public string ViewTitle
        {
            get
            {
                return viewTitle;
            }
            set
            {
                if (viewTitle != value)
                {
                    viewTitle = value;
                    OnPropertyChanged("ViewTitle");
                }
            }
        }

        #endregion

        /// <summary>
        /// 显示错误信息
        /// </summary>
        /// <param name="error">错误信息</param>
        protected virtual void ShowError(string error)
        {
            Error = error;
            IPop<string> pop = ClientViewModelService.Current.GetPop(Pop.错误信息) as IPop<string>;
            pop.Info = error;
            pop.Show();
        }

        protected virtual void ShowMsg(string msg)
        {
            this.Msg = msg;
            ShowError(msg);
        }

    }
}
